package org.budo.filestore.impl;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.budo.filestore.api.UploadRequest;
import org.budo.support.lang.util.IoUtil;

import lombok.Getter;
import lombok.Setter;

/**
 * @author lmw
 */
@Getter
@Setter
public class DefaultUploadRequest extends AbstractRequest implements UploadRequest, Serializable {
    private static final long serialVersionUID = -8436783179779430562L;

    private InputStream inputStream;

    private Map<String, Object> metadata = new HashMap<String, Object>();

    public DefaultUploadRequest() {
    }

    public DefaultUploadRequest(String contentUrl) {
        this.setContentUrl(contentUrl);
    }

    public DefaultUploadRequest(String contentUrl, InputStream inputStream) {
        this.setContentUrl(contentUrl);

        this.inputStream = inputStream;
    }

    public DefaultUploadRequest(String contentUrl, InputStream inputStream, Map<String, Object> metadata) {
        this(contentUrl, inputStream);
        this.metadata = metadata;
    }

    public DefaultUploadRequest(String contentUrl, byte[] bytes) {
        this(contentUrl, null == bytes ? null : new ByteArrayInputStream(bytes));
    }

    public DefaultUploadRequest(String contentUrl, File file) {
        this(contentUrl, IoUtil.fileInputStream(file));
    }

    public String getContentType() {
        return (String) this.metadata.get("Content-Type");
    }

    public void setContentType(String contentType) {
        this.metadata.put("Content-Type", contentType);
    }

    public String getContentEncoding() {
        return (String) this.metadata.get("Content-Encoding");
    }

    public void setContentEncoding(String encoding) {
        this.metadata.put("Content-Encoding", encoding);
    }

    public String getCacheControl() {
        return (String) this.metadata.get("Cache-Control");
    }

    public void setCacheControl(String cacheControl) {
        this.metadata.put("Cache-Control", cacheControl);
    }

    public String getContentDisposition() {
        return (String) this.metadata.get("Content-Disposition");
    }

    public void setContentDisposition(String disposition) {
        this.metadata.put("Content-Disposition", disposition);
    }

    public long getContentLength() {
        Long contentLength = (Long) this.metadata.get("Content-Length");
        return contentLength == null ? 0L : contentLength;
    }

    public void setContentLength(long contentLength) {
        this.metadata.put("Content-Length", contentLength);
    }

    @Override
    public String toString() {
        return super.toString() //
                + ", contentUrl=" + this.getContentUrl() //
                + ", inputStream=" + this.getInputStream() //
                + ", metadata=" + this.getMetadata();
    }
}
